package com.stylefeng.guns.modular.netcar.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.DispatchPsnDriverMapper;
import com.stylefeng.guns.modular.netcar.model.DispatchPsnDriver;
import com.stylefeng.guns.modular.netcar.service.IDispatchPsnDriverService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 调度员可调度驾驶员 Service 实现 
 * @author sasazaixian
 * @Date 2018-11-15 10:28
 */
@Service
public class DispatchPsnDriverServiceImpl extends ServiceImpl<DispatchPsnDriverMapper,DispatchPsnDriver> implements IDispatchPsnDriverService {

    @Resource
    private DispatchPsnDriverMapper dispatchPsnDriverMapper;

    /**
     * 删除调度员可调度驾驶员
     * @param dispatchPsnDriver
     * @return     
     */
    @Transactional
    public int deleteDispatchPsnDriver(DispatchPsnDriver dispatchPsnDriver,String token)throws Exception{
        if(dispatchPsnDriver==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.dispatchPsnDriverMapper.deleteDispatchPsnDriver(dispatchPsnDriver);
    }
    /**
     * 批量删除调度员可调度驾驶员
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchDispatchPsnDriver(String[] ids,String token)throws Exception{
        return this.dispatchPsnDriverMapper.deleteBatchDispatchPsnDriver(ids);
    }
    /**
     * 查询调度员可调度驾驶员列表
     * @param dispatchPsnDriver 调度员可调度驾驶员对象
     * @return
     */
    public List<DispatchPsnDriver> selectListDispatchPsnDriver(DispatchPsnDriver dispatchPsnDriver,String token)throws Exception{
        return  this.dispatchPsnDriverMapper.selectListDispatchPsnDriver(dispatchPsnDriver);
    }
    /**
	 * 分页查询调度员可调度驾驶员列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListDispatchPsnDriver(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(dispatchPsnDriverMapper.selectListDispatchPsnDriver(page,(DispatchPsnDriver)pager.getObj()));
        List<DispatchPsnDriver> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param dispatchPsnDriver 调度员可调度驾驶员对象
     * @return 
     */
    public  List<DispatchPsnDriver> selectListDispatchPsnDriverForUpdate(DispatchPsnDriver dispatchPsnDriver)throws Exception{
        return  this.dispatchPsnDriverMapper.selectListDispatchPsnDriverForUpdate(dispatchPsnDriver);
    }
    /**
     * 通过主键id查询的调度员可调度驾驶员列表
     * @param id 主键id
     * @return     
     */
    public  DispatchPsnDriver findDispatchPsnDriverById(String id,String token)throws Exception{
        return  this.dispatchPsnDriverMapper.findDispatchPsnDriverById(id);
    }

    /**
     * 新增调度员可调度驾驶员数据
     * @param dispatchPsnDriver  调度员可调度驾驶员对象
     * @return
     */
    @Transactional
    public int insertDispatchPsnDriver(DispatchPsnDriver dispatchPsnDriver,String token) throws Exception {

        if((dispatchPsnDriver.getDiViArryObj()==null || dispatchPsnDriver.getDiViArryObj().trim().length()<10)
                && (dispatchPsnDriver.getDiArry()==null || dispatchPsnDriver.getDiArry().trim().length()<10)){
            throw new RuntimeException("驾驶员不能为空！");
        }
        if(dispatchPsnDriver.getSuId()==null && (dispatchPsnDriver.getSuIdArry()==null || dispatchPsnDriver.getSuIdArry().trim().length()<1)){
            throw new RuntimeException("线路不能为空！");
        }
        JSONArray jsonArray=null;
        String[]  diArry=null;
        if(dispatchPsnDriver.getDiViArryObj()!=null && dispatchPsnDriver.getDiViArryObj().trim().length()>10){
            jsonArray = JSONArray.parseArray(dispatchPsnDriver.getDiViArryObj().trim());
        }else{
            diArry=dispatchPsnDriver.getDiArry().trim().split(",");
        }

        String [] suIdArry=null;
        if(dispatchPsnDriver.getSuId()==null){
            suIdArry= dispatchPsnDriver.getSuIdArry().split(",");
        }else{
            suIdArry=new String []{dispatchPsnDriver.getSuId()};
        }
        for(String suid:suIdArry){
            dispatchPsnDriver.setSuId(suid);
            if(jsonArray!=null){
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject json = jsonArray.getJSONObject(i);
                    String diId = json.getString("diId").trim();
                    if (diId == null || diId.length() < 10) {
                        continue;
                    }
                    String viId = "";
                    if (json.containsKey("viId")) {
                        viId = json.getString("viId").trim();
                    }
                    dispatchPsnDriver.setDpdId(IdWorker.get32UUID());
                    dispatchPsnDriver.setDiId(diId);
                    if (viId != null && viId.length() > 10) {
                        dispatchPsnDriver.setViId(viId);
                    }
                    this.dispatchPsnDriverMapper.insertDispatchPsnDriver(dispatchPsnDriver);
                }
            }else{
                for(String diId:diArry){
                    dispatchPsnDriver.setDpdId(IdWorker.get32UUID());
                    dispatchPsnDriver.setDiId(diId);
                    this.dispatchPsnDriverMapper.insertDispatchPsnDriver(dispatchPsnDriver);
                }
            }

        }
        return 0;
    }
    
    /**
     * 更新调度员可调度驾驶员数据
     * @param dispatchPsnDriver 调度员可调度驾驶员对象
     * @return     
     */
    @Transactional
    public int updateDispatchPsnDriver(DispatchPsnDriver dispatchPsnDriver,String token) throws Exception {
        if(dispatchPsnDriver==null || dispatchPsnDriver.getViId()==null || dispatchPsnDriver.getViId().length()<10){
            throw new RuntimeException("车辆不能为空！");
        }
        DispatchPsnDriver updateDispatchPsnDriver=new DispatchPsnDriver();
        updateDispatchPsnDriver.setViId(dispatchPsnDriver.getViId());
        updateDispatchPsnDriver.setDpdId(dispatchPsnDriver.getDpdId());
        return  this.dispatchPsnDriverMapper.updateDispatchPsnDriver(dispatchPsnDriver);
    }    
    /**
     * 查询调度员可调度驾驶员返回key-Value属性
     * @param dispatchPsnDriver 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsDispatchPsnDriver(DispatchPsnDriver dispatchPsnDriver,String token) throws Exception {
        List<DispatchPsnDriver> listDispatchPsnDriver=this.dispatchPsnDriverMapper.selectListDispatchPsnDriver(dispatchPsnDriver);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(DispatchPsnDriver item:listDispatchPsnDriver){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getDpdId());
            opts.setValue(item.getDpdId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
